/*
 * SilkAfxAccess.java
 * 
 * Created on Mar 13,2006
 * David Connell
 * Copyright (c) 2006 Synergy Systems, Inc
 */
package org.puresilk.afx.util;

// java support
import java.io.Serializable;
import java.util.*;


/**
 * Represents a user's authentication 
 * token.
 * 
 * @author David Connell
 */
public class SilkAfxAccess implements Serializable  {
	private static final long serialVersionUID = 3192186033683521156L;
	
	// class attributes
	private int uKey = 0;
	private int home = 0;
	private String uid = null;
	private String pwd = null;
	private boolean authenticated = false;
	private Vector<Integer> roles = null;
	
	/**
	 * Public Constructor
	 *  
	 * @param logon
	 * @param password
	 */
	public SilkAfxAccess (String logon, String password) {
		uKey = 0;
		home = 0;
		uid = logon;
		pwd = password;
		roles = new Vector<Integer>();
	}

	/**
	 * Default constructor.
	 */
	public SilkAfxAccess (){
		uKey = 0;
		home = 0;
		uid = "";
		pwd = "";
		roles = new Vector<Integer>();
	}
	
	/**
	 * Authenticate user.
	 * 
	 * @param uRoles
	 * @param rkey
	 */
	public void authenticate() {
		// set ok flag
		authenticated = true;		
	}
	
	/**
	 * Check logon status 
	 * @return
	 */
	public boolean isAuthenticated() {
		return authenticated;
	}

	/**
	 * Get the user's logon id
	 * @return
	 */
	public String getUid() {
		return uid;
	}

	/**
	 * @param uid The uid to set.
	 */
	public void setUid(String uid) {
		this.uid = uid;
	}

	/**
	 * @return Returns the pwd.
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd The pwd to set.
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @return Returns the uKey.
	 */
	public int getUserKey() {
		return uKey;
	}

	/**
	 * @param key The uKey to set.
	 */
	public void setUserKey(int key) {
		uKey = key;
	}

	/**
	 * Check access for presence of a role.
	 * 
	 * @param role
	 * @return True if fole found
	 */
	public boolean hasRole(int role){
		return hasRole(new Integer(role));
	}
	
	/**
	 * Check access for presence of a role.
	 * 
	 * @param role
	 * @return True if role found
	 */
	public boolean hasRole(Integer role){
		Iterator i = roles.iterator();
		// look in roles vector
		while (i.hasNext())
			if(role.equals((Integer)i.next()))
				return true;
		
		// not found
		return false;			
	}

	/**
	 * @return Returns the home.
	 */
	public int getHome() {
		return home;
	}

	/**
	 * @param home The home to set.
	 */
	public void setHome(int home) {
		this.home = home;
	}
}
